import numpy as np

def F6_MPC_Matrices_Constraints(x_low, x_high, u_low, u_high, N_p, Phi, Gamma):
    # 系统状态维度和输入维度
    n = x_low.shape[0]
    p = u_low.shape[0]

    M = np.vstack((np.zeros((p, n)), np.zeros((p, n)), -np.eye(n), np.eye(n)))
    F = np.vstack((-np.eye(p), np.eye(p), np.zeros((n, p)), np.zeros((n, p))))
    Beta = np.vstack((-u_low, u_high, -x_low, x_high))

    M_Np = M[-2 * n:]
    Beta_N = Beta[-2 * n:]

    M_bar = np.zeros(((2 * n + 2 * p) * N_p + 2 * n, n))
    M_bar[: (2 * n + 2 * p)] = M

    Beta_bar = np.zeros(((2 * n + 2 * p) * N_p + 2 * n, 1))
    Beta_bar[: (2 * n + 2 * p) * N_p] = np.tile(Beta, (N_p, 1))
    Beta_bar[-2 * n:] = Beta_N

    M_2bar = np.zeros(((2 * n + 2 * p) * N_p + 2 * n, n * N_p))
    for i in range(N_p):
        M_2bar[(2 * n + 2 * p) * i: (2 * n + 2 * p) * (i + 1), n * i: n * (i + 1)] = M
    M_2bar[-2 * n:, -n:] = M_Np

    F_2bar = np.zeros(((2 * n + 2 * p) * N_p + 2 * n, p * N_p))
    for i in range(N_p):
        F_2bar[(2 * n + 2 * p) * i: (2 * n + 2 * p) * (i + 1), p * i: p * (i + 1)] = F
    F_2bar[-2 * n:, -p:] = np.zeros((2 * n, p))
    b = -(M_bar + np.matmul(M_2bar, Phi))
    M_final = np.matmul(M_2bar, Gamma) + F_2bar

    return  M_final, Beta_bar, b
